package com.smallkingson.ylib.core.utils.search;

import android.widget.TextView;

import com.smallkingson.ylib.view.utils.YViews;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/3/20.
 */
public class YSearchs {

    public static <T extends Searchable> void search(List<T> searchables, String keyWord, String[]
            searchFields, SearchCallback searchCallback) {

        List<T> results = new ArrayList<>();

        for (T t : searchables) {
            Map<String, YSearchs.SearchHolder> searchHolders = new HashMap<>();
            boolean isSearched = false;
            for (String searchField : searchFields) {
                String value = t.search(searchField, keyWord);
                if (value != null) {
                    int start = value.indexOf(keyWord);
                    if (start >= 0) {
                        searchHolders.put(searchField, new YSearchs.SearchHolder(searchCallback.handleValue(value), keyWord, start, start + keyWord
                                .length
                                        ()));
                        t.hold(searchHolders);
                        isSearched = true;
                    }
                }
            }
            if (isSearched) {
                results.add(t);
            }
        }
        searchCallback.onSearchResult(results);
    }

    public static interface SearchCallback<T> {
        void onSearchResult(List<T> results);

        /**
         * 有些情况需要吧value转成span
         * @param value
         * @return
         */
        CharSequence handleValue(String value);
    }

    public static void setSpan(Map<String, YSearchs.SearchHolder> holderMap, TextView textView, Searchable
            searchable, String field, CharSequence defaultText, int color) {
        if (holderMap == null) {
            return;
        }
        SearchHolder searchHolder = holderMap.get(field);
        if (searchHolder==null) {
            textView.setText(defaultText);
        }else{
            YViews.SpanColorHolder spanColorHolder = new YViews.SpanColorHolder(searchHolder.start, searchHolder.end, color);
            YViews.setTextViewColorSpan(textView, holderMap.get(field).value, spanColorHolder);
        }
    }

    public static class SearchHolder {
        public CharSequence value;
        public String keyWord;
        public int start;
        public int end;

        public SearchHolder(CharSequence value, String keyWord, int start, int end) {
            this.value = value;
            this.keyWord = keyWord;
            this.start = start;
            this.end = end;
        }

        @Override
        public String toString() {
            return "SearchHolder{" +
                    "value='" + value + '\'' +
                    ", keyWord='" + keyWord + '\'' +
                    ", start=" + start +
                    ", end=" + end +
                    '}';
        }
    }

}
